
"""
 * Copyright 2009 Yogi KENT
 * 
 * This file is main part of Sotetsu.
 * 
 * Sotetsu is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * Sotetsu is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.

"""

import threading
import curses
import curses.panel
import logging

#from sotetsu_pre import *
from ._sotetsu_utils import *
from ._sotetsu import *
from .daap import daapclient
from .player import simpleplayer
from . import wcwidth


class Playlist:
	"""this class manages playlist.

	it is used by simpleplayer.Player object as to be
	iterator of file object.
	each next calling returns one mp3 fileobject.
	in the beginning, current position of itemlist was
	changed by other thread, but it has not been used now.
	"""
	def __init__(self, daap, items, pos=0):
		self.itemList = items
		self.pos = pos
		self.daap = daap
		self.fo = None
		self.item = None
		self._lock = threading.RLock()
	def __next__(self):
		if self.fo!=None:
			self.fo.close()
		self._lock.acquire()	# Lock
		if self.pos==len(self.itemList):
			raise StopIteration()
		self.item = self.itemList[self.pos]
		self.fo = self.daap.getStream(self.item)
		self.pos += 1
		self._lock.release()	# UnLock
		return self.fo
	def __iter__(self):
		return self

	def setNext(self, pos):
		"""set next position to be played."""
		# we have to implement synchronization.
		self._lock.acquire()	# Lock
		self.pos = pos
		self._lock.release()	# UnLock
	def lastItem(self):
		return self.item

	# lock primitive
	def lock(self):
		self._lock.acquire()
	def unlock(self):
		self._lock.release()




